<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';

import type { VxeGridProps } from '#/adapter/vxe-table';
import type { OperateLog } from '#/api/system/log/model';

import { Page, useVbenModal } from '@vben/common-ui';

import { Space } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { logList } from '#/api/system/log';

import { columns, querySchema } from './data';
import logModal from './log-modal.vue';

const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
      class: 'w-full',
    },
  },
  // 日期选择格式化
  fieldMappingTime: [
    [
      'operateTime',
      ['startDate', 'endDate'],
      ['YYYY-MM-DD 00:00:00', 'YYYY-MM-DD 23:59:59'],
    ],
  ],
  schema: querySchema(),
  showCollapseButton: false,
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
};

const gridOptions: VxeGridProps = {
  columns,
  height: 'auto',
  keepSource: true,
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        const data = await logList({
          pageNo: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
        return data;
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  id: 'system-log-index',
};

const [BasicTable] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});

const [LogModal, modalApi] = useVbenModal({
  connectedComponent: logModal,
});

async function handleView(record: OperateLog) {
  modalApi.setData(record).open();
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="操作日志列表">
      <template #action="{ row }">
        <Space>
          <ghost-button
            @click.stop="handleView(row)"
            v-access:code="['sys:operate:query']"
          >
            查看
          </ghost-button>
        </Space>
      </template>
    </BasicTable>
    <LogModal />
  </Page>
</template>
